import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { SysUser } from '#/api/user/sysuser';

import { useAccess } from '@vben/access';

import { z } from '#/adapter/form';
import { COMMON_DICT, USER_DICT } from '#/api';
import { deptTreeSelect } from '#/api/user/sysuser';
import { $t } from '#/locales';

const { hasAccessByCodes } = useAccess();

/**
 * 查询参数
 */
export function querySchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('user.name'),
    },
    {
      component: 'ApiTreeSelect',
      componentProps: {
        defaultExpandAll: true,
        checkStrictly: true,
        allowClear: true,
        api: deptTreeSelect,
        class: 'w-full',
        labelField: 'label',
        valueField: 'id',
        childrenField: 'children',
      },
      fieldName: 'deptId',
      label: $t('dept.dept'),
    },
    {
      component: 'ApiSelect',
      componentProps: {
        buttonStyle: 'solid',
        clearable: true,
        options: [
          { label: $t('common.enabled'), value: COMMON_DICT.STATUS_ENABLE },
          { label: $t('common.disabled'), value: COMMON_DICT.STATUS_DISABLE },
        ],
        optionType: 'button',
      },
      defaultValue: null,
      fieldName: 'status',
      label: $t('common.status'),
    },
  ];
}

/**
 * 获取编辑表单的字段配置。如果没有使用多语言，可以直接export一个数组常量
 */
export function formSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('user.name'),
      rules: z
        .string()
        .min(6, $t('ui.formRules.minLength', [$t('user.name'), 6]))
        .max(20, $t('ui.formRules.maxLength', [$t('user.name'), 20])),
    },
    {
      component: 'ApiTreeSelect',
      componentProps: {
        defaultExpandAll: true,
        checkStrictly: true,
        allowClear: true,
        api: deptTreeSelect,
        class: 'w-full',
        labelField: 'label',
        valueField: 'id',
        childrenField: 'children',
      },
      fieldName: 'deptId',
      label: $t('dept.dept'),
    },
    {
      component: 'Input',
      fieldName: 'nickname',
      label: $t('user.nickname'),
      rules: z
        .string()
        .min(0, $t('ui.formRules.minLength', [$t('user.nickname'), 0]))
        .max(30, $t('ui.formRules.maxLength', [$t('user.nickname'), 30]))
        .nullish(),
    },
    {
      component: 'Input',
      fieldName: 'roleIds',
      label: $t('role.role'),
    },
    {
      component: 'Input',
      fieldName: 'phoneNumber',
      label: $t('user.phoneNumber'),
      rules: z
        .string()
        .min(0, $t('ui.formRules.minLength', [$t('user.phoneNumber'), 0]))
        .max(16, $t('ui.formRules.maxLength', [$t('user.phoneNumber'), 16]))
        .nullish(),
    },
    {
      component: 'Input',
      fieldName: 'postIds',
      label: $t('post.post'),
    },
    {
      component: 'Input',
      fieldName: 'email',
      label: $t('user.email'),
      rules: z
        .string()
        .min(0, $t('ui.formRules.minLength', [$t('user.email'), 0]))
        .max(50, $t('ui.formRules.maxLength', [$t('user.email'), 50]))
        .nullish(),
    },
    {
      component: 'Select',
      componentProps: {
        options: [
          { label: $t('common.man'), value: USER_DICT.GENDER_MAN },
          { label: $t('common.woman'), value: USER_DICT.GENDER_WOMAN },
          { label: $t('common.unknown'), value: USER_DICT.GENDER_UNKNOWN },
        ],
      },
      defaultValue: USER_DICT.GENDER_UNKNOWN,
      fieldName: 'gender',
      label: $t('user.gender'),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: COMMON_DICT.STATUS_ENABLE },
          { label: $t('common.disabled'), value: COMMON_DICT.STATUS_DISABLE },
        ],
        optionType: 'button',
      },
      defaultValue: COMMON_DICT.STATUS_ENABLE,
      fieldName: 'status',
      label: $t('common.status'),
    },
    {
      component: 'Input',
      componentProps: {
        type: 'textarea',
      },
      fieldName: 'remark',
      label: $t('common.remark'),
      formItemClass: 'col-span-2 md:col-span-2',
    },
  ];
}

/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export function columnsSchema<T>(
  onStatusChange?: (
    newStatus: string,
    row: T,
  ) => PromiseLike<boolean | undefined>,
  onActionClick?: OnActionClickFn<SysUser>,
): VxeTableGridOptions<SysUser>['columns'] {
  return [
    { title: '序号', type: 'seq', width: 50 },
    {
      field: 'username',
      title: $t('user.name'),
    },
    {
      cellRender: {
        name: 'CellAvatar',
      },
      field: 'avatar',
      title: $t('user.avatar'),
    },
    {
      field: 'nickname',
      title: $t('user.nickname'),
    },
    {
      field: 'deptName',
      title: $t('dept.dept'),
    },
    {
      field: 'phoneNumber',
      title: $t('user.phoneNumber'),
    },
    {
      cellRender: {
        attrs: { beforeChange: onStatusChange },
        name: 'CellSwitch',
      },
      field: 'status',
      title: $t('common.status'),
    },
    {
      field: 'createAt',
      title: $t('common.createTime'),
    },
    {
      cellRender: {
        attrs: {
          nameField: 'username',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'edit',
            show: () => {
              return hasAccessByCodes(['sys_user:edit']);
            },
          },
          {
            code: 'delete',
            text: true,
            show: () => {
              return hasAccessByCodes(['sys_user:remove']);
            },
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      align: 'center',
      headerAlign: 'center',
      showOverflow: false,
      title: $t('page.action.action'),
    },
  ];
}
